package in.muton.edushare.documentModel.excel;

import java.util.HashMap;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;

/**
 * <p>
 * StylePalette class main purpose is to keep low memory footprint 
 * by reusing CellStyles throughout the workbook creation. It uses 
 * HashMap to store Styles
 * </p>
 * @author naishe
 *
 */
public class StylePalette {
	
	private HSSFWorkbook workbook;
	public static enum CELL_STYLE{
		LONG_TEXT,
		TABLE_HEADER,
		BOLD,
		H1,
		H2,
		H3,
		ALERT,
		HIGHLIGHT, 
		H4
	}
	private final HashMap<CELL_STYLE, CellStyle> styleMap = new HashMap<CELL_STYLE, CellStyle>();
	
	public StylePalette(HSSFWorkbook workbook) {
		this.workbook = workbook;
	}
	
	/** 
	 * <p>TODO: Big font texts like h1 through h3 
	 * are needed to be fixed. height 
	 * is not getting updated according to font size.</p>
	 * @param style
	 * @return
	 */
	public CellStyle getStyle(CELL_STYLE style){
		
		switch (style) {
		case LONG_TEXT:
			return getWrapTextStyle();
		case TABLE_HEADER:
			return getTableHeaderStyle();
		case BOLD:
			return getBoldStyle();
		case H1:
			return getH1Style();
		case H2:
			return getH2Style();
		case H3:
			return getH3Style();
		case H4:
			return getH4Style();
		case ALERT:
			return getAlertStyle();
		case HIGHLIGHT:
			return getHighlightStyle();
		default:
			return null;
		}
	}

	private CellStyle getH4Style() {
		if(styleMap.get(CELL_STYLE.H4)==null){
			CellStyle cs = workbook.createCellStyle();
			cs.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
			cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
			cs.setBorderBottom(CellStyle.BORDER_THIN);
			Font font = workbook.createFont();
			font.setBoldweight(Font.BOLDWEIGHT_BOLD);
			cs.setFont(font);
			
			styleMap.put(CELL_STYLE.H4, cs);
		}
		return styleMap.get(CELL_STYLE.H4);
	}

	private CellStyle getHighlightStyle() {
		if(styleMap.get(CELL_STYLE.HIGHLIGHT)==null){
			CellStyle cs = workbook.createCellStyle();
			cs.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());
			cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
			cs.setBorderBottom(CellStyle.BORDER_DASHED);
			
			Font font = workbook.createFont();
			font.setItalic(true);
			cs.setFont(font);
			
			styleMap.put(CELL_STYLE.HIGHLIGHT, cs);
		}
		return styleMap.get(CELL_STYLE.HIGHLIGHT);
	}

	private CellStyle getAlertStyle() {
		if(styleMap.get(CELL_STYLE.ALERT)==null){
			CellStyle cs = workbook.createCellStyle();
			cs.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
			cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
			cs.setBorderBottom(CellStyle.BORDER_DASHED);
			cs.setBorderLeft(CellStyle.BORDER_DASHED);
			cs.setBorderTop(CellStyle.BORDER_DASHED);
			cs.setBorderRight(CellStyle.BORDER_DASHED);
			
			Font font = workbook.createFont();
			font.setItalic(true);
			cs.setFont(font);
			
			styleMap.put(CELL_STYLE.ALERT, cs);
		}
		return styleMap.get(CELL_STYLE.ALERT);
	}

	private CellStyle getH3Style() {
		if(styleMap.get(CELL_STYLE.H3)==null){
			CellStyle cs = workbook.createCellStyle();
			cs.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
			cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
			cs.setBorderBottom(CellStyle.BORDER_THIN);
			Font font = workbook.createFont();
//			font.setFontHeightInPoints((short)12);
			font.setBoldweight(Font.BOLDWEIGHT_BOLD);
			cs.setFont(font);
			
			styleMap.put(CELL_STYLE.H3, cs);
		}
		return styleMap.get(CELL_STYLE.H3);
	}
	
	private CellStyle getH2Style() {
		if(styleMap.get(CELL_STYLE.H2)==null){
			CellStyle cs = workbook.createCellStyle();
			cs.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
			cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
			cs.setBorderBottom(CellStyle.BORDER_THIN);
			Font font = workbook.createFont();
//			font.setFontHeightInPoints((short)14);
			font.setBoldweight(Font.BOLDWEIGHT_BOLD);
			cs.setFont(font);
			
			styleMap.put(CELL_STYLE.H2, cs);
		}
		return styleMap.get(CELL_STYLE.H2);
	}

	private CellStyle getH1Style() {
		if(styleMap.get(CELL_STYLE.H1)==null){
			CellStyle cs = workbook.createCellStyle();
			cs.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
			cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
			cs.setBorderBottom(CellStyle.BORDER_THIN);
			Font font = workbook.createFont();
//			font.setFontHeightInPoints((short)16);
			font.setBoldweight(Font.BOLDWEIGHT_BOLD);
			cs.setFont(font);
			
			styleMap.put(CELL_STYLE.H1, cs);
		}
		return styleMap.get(CELL_STYLE.H1);
	}

	private CellStyle getBoldStyle() {
		if(styleMap.get(CELL_STYLE.BOLD)==null){
			CellStyle cs = workbook.createCellStyle();
			Font font = workbook.createFont();
			font.setBoldweight(Font.BOLDWEIGHT_BOLD);
			cs.setFont(font);
			styleMap.put(CELL_STYLE.BOLD, cs);
		}
		return styleMap.get(CELL_STYLE.BOLD);
	}

	private CellStyle getTableHeaderStyle() {
		if(styleMap.get(CELL_STYLE.TABLE_HEADER)==null){
			CellStyle cs = workbook.createCellStyle();
			cs.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());
			cs.setFillPattern(CellStyle.SOLID_FOREGROUND);
			cs.setBorderBottom(CellStyle.BORDER_THIN);
			Font font = workbook.createFont();
			font.setBoldweight(Font.BOLDWEIGHT_BOLD);
			cs.setFont(font);
			
			styleMap.put(CELL_STYLE.TABLE_HEADER, cs);
		}
		return styleMap.get(CELL_STYLE.TABLE_HEADER);
	}

	private CellStyle getWrapTextStyle() {
		if(styleMap.get(CELL_STYLE.LONG_TEXT)==null){
			CellStyle cs = workbook.createCellStyle();
			 cs.setWrapText(true);
			styleMap.put(CELL_STYLE.LONG_TEXT, cs);
		}
		return styleMap.get(CELL_STYLE.LONG_TEXT);
	}

}
